<template>
  <div class="flex flexColumn flex1">
    <div class="title">
      <span>首页 &gt; 店铺管理 </span>
    </div>

    <div class="main flex flexColumn">
      <div class="main-title">店铺管理</div>
      <div>
        <el-form inline label-width="80px" class="flex flexColumn from">
          <el-form-item label="店铺名称">
            <el-input v-model="obj.name" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="店铺公告">
            <el-input
              type="textarea"
              v-model="obj.bulletin"
              rows="6"
              style="width: 400px"
            ></el-input>
          </el-form-item>
          <div class="flex storeform-img">
            <p>店铺头像</p>
            <el-upload
              class="avatar-uploader"
              :on-success="handleAvatarSuccess"
              action="http://127.0.0.1:5000/shop/upload"
              :show-file-list="false"
            >
              <img v-if="obj.avatar" :src="obj.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
          <div class="flex storeform-img">
            <p>店铺图片</p>
            <div class="images flex">
              <el-upload
                action="http://127.0.0.1:5000/shop/upload"
                list-type="picture-card"
                :on-success="storeSuccess"
                :on-remove="handleRemove"
                :on-preview="handlePictureCardPreview"
                :file-list="obj.pics"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </div>
          </div>
          <el-form-item label="配送费">
            <el-input v-model="obj.deliveryPrice" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="配送时间">
            <el-input v-model="obj.deliveryTime" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="最低消费">
            <el-input v-model="obj.minPrice" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="配送描述">
            <el-input v-model="obj.description" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="配送评分">
            <el-input v-model="obj.score" clearable=""></el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input v-model="obj.sellCount" clearable=""></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox-group v-model="checkSupports" class="flex">
              <p>活动</p>
              <div class="images">
                <el-checkbox
                  v-for="Support in supports"
                  :label="Support"
                  :key="Support"
                  >{{ Support }}</el-checkbox
                >
              </div>
            </el-checkbox-group>
          </el-form-item>
          <div class="flex">
            <p>添加活动</p>
            <el-input
              v-model="addCheck"
              placeholder="添加活动"
              style="width: 300px; margin-right: 20px"
            ></el-input>
            <el-button type="goon" @click="addActive">添加活动</el-button>
          </div>
          <div class="flex storeform-timer">
            <p>营业时间</p>
            <el-time-picker
              is-range
              v-model="obj.date"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
            >
            </el-time-picker>
          </div>
          <el-button type="goon" style="margin-top: 20px" @click="editStore">
            修改店铺
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { info, edit } from "@/apis/store.js";
import { getYYYYMMDDHHMMSS } from "@/utlis/utils.js";

export default {
  data() {
    return {
      name: "",
      obj: {},
      desc: "",
      imageUrl: "",
      avatarImg: "",
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      checkSupports: [],
      addCheck: "",
      supports: [],
    };
  },
  created() {
    this.reStore();
  },

  methods: {
    reStore() {
      info().then((res) => {
        if (res.status == 200) {
          res.data.data.pics = res.data.data.pics.map((v) => {
            return { name: v.substr(v.lastIndexOf("/") + 1), url: v };
          });
          this.obj = res.data.data;
          this.checkSupports = this.obj.supports;
          this.supports = JSON.parse(JSON.stringify(this.obj.supports));
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.obj.avatar = URL.createObjectURL(file.raw);
      this.avatarImg = res.imgUrl;
    },
    handleRemove(file, filelist) {
      this.obj.pics = filelist;
    },
    storeSuccess(res, file, filelist) {
      this.obj.pics = filelist;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    editStore() {
      let date = [];
      let pics = [];
      this.avatarImg = this.avatarImg
        ? this.avatarImg
        : this.obj.avatar.substr(this.obj.avatar.lastIndexOf("/") + 1);
      pics = this.obj.pics.map((obj) =>
        obj.response
          ? obj.response.imgUrl
          : obj.url.substr(obj.url.lastIndexOf("/") + 1)
      );
      date.push(getYYYYMMDDHHMMSS(this.obj.date[0]));
      date.push(getYYYYMMDDHHMMSS(this.obj.date[1]));
      edit({
        id: this.obj.id,
        name: this.obj.name,
        bulletin: this.obj.bulletin,
        avatar: this.avatarImg,
        deliveryPrice: Number(Math.floor(this.obj.deliveryPrice)),
        deliveryTime: Number(Math.floor(this.obj.deliveryTime)),
        description: this.obj.description,
        score: Number(this.obj.score).toFixed(1),
        sellCount: Number(Math.floor(this.obj.sellCount)),
        supports: JSON.stringify(this.checkSupports),
        pics: JSON.stringify(pics),
        date: JSON.stringify(date),
      }).then((res) => {
        console.log(res);
        this.$message({
          type: "success",
          message: "优选管理系统提示: " + this.obj.name + " 店铺信息修改成功!",
        });
      });
    },
    addActive() {
      this.supports.push(this.addCheck);
    },
  },
};
</script>

<style lang="less" scoped>
.storeform-timer {
  margin-top: 20px;
}

.storeform-img {
  padding-bottom: 20px;
}
[class*=" el-icon-"],
[class^="el-icon-"] {
  line-height: 178px;
}
.images {
  width: 600px;
  flex-wrap: wrap;
}
p {
  font-size: 14px;
  color: #606266;
  padding-right: 12px;
  text-align: right;
  width: 70px;
}
.el-button--goon {
  color: #fff;
  background-color: #706c61;
  border-color: #706c61;
}
.el-button--light {
  color: #706c61;
  background-color: #e1f4f3;
  border-color: #e1f4f3;
}
</style>